package com.ly.hotel.view.common;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import androidx.annotation.NonNull;
import com.ly.hotel.R;

public class LegalDeclarationDialog extends Dialog {

	public LegalDeclarationDialog(Context context) {
		super(context);
	}

	public LegalDeclarationDialog(Context context, int theme) {
		super(context, theme);
	}

	@Override
	public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT ||
				keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_DOWN ||
				keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			return super.onKeyDown(keyCode, event);
		}
		return true;
	}

	public static class Builder {
		private Context context;

		private OnClickListener firstButtonClickListener;
		private OnClickListener secondButtonClickListener;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setFirstButton(OnClickListener listener) {
			this.firstButtonClickListener = listener;
			return this;
		}

		public Builder setSecondButton(OnClickListener listener) {
			this.secondButtonClickListener = listener;
			return this;
		}

		public LegalDeclarationDialog create(boolean tag) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final LegalDeclarationDialog dialog = new LegalDeclarationDialog(context, R.style.Dialog);
			View layout = inflater.inflate(R.layout.legal_declaration_layout, null);
			dialog.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

			final Button firstBtn = layout.findViewById(R.id.legal_disagree);
			if (firstButtonClickListener != null) {
				firstBtn.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						firstButtonClickListener.onClick(dialog, 1);
					}
				});
			}


			Button secondBtn = layout.findViewById(R.id.legal_agree);
			if (secondButtonClickListener != null) {
				secondBtn.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						secondButtonClickListener.onClick(dialog, 2);
					}
				});
			}

			secondBtn.post(new Runnable(){
				public void run(){
					firstBtn.requestFocus();
					firstBtn.requestFocusFromTouch();
				}
			});

			dialog.setCanceledOnTouchOutside(tag);
			dialog.setContentView(layout);
			return dialog;
		}
	}
}
